from Products.Archetypes.Widget import TypesWidget
from Products.Archetypes.Registry import registerWidget

class DIYWidget(TypesWidget):
    _properties = TypesWidget._properties.copy()
    _properties.update({
        'macro' : "widget_diy",
        'diy_method': '', # method that renders the widget in edit mod
        'diy_view': '', # method that renders the widget in view mod
        })


__all__ = ('DIYWidget',)


registerWidget(DIYWidget,
               title='Do It Yourself Widget',
               description='Lets a method or function render the widget for speed',
               used_for=()
               )


#############################
# fast, big selectbox widgets

from sets import Set
from cStringIO import StringIO

def selectBox(name, items, value, **args):
    f = StringIO()
    params = []
    for item in args.items():
        params.append('%s="%s"' % item)
    f.write('<select name="%s" %s>' % (name, ' '.join(params)))
    f.write('<option value="">-- ? --</option>')
    for key, val in items:
        if key == value:
            f.write('<option value="%s" selected="selected">%s</option>'  % (key, val))
        else:
            f.write('<option value="%s">%s</option>'  % (key, val))
    f.write('</select>')
    return f.getvalue()


def multiSelectBox(name, items, values, **args):
    values = Set(values)
    f = StringIO()
    params = []
    for item in args.items():
        params.append('%s="%s"' % item)
    f.write('<select name="%s:list" %s multiple>' % (name, ' '.join(params)))
    f.write('<option value="">-- ? --</option>')
    for key, val in items:
        if key in values:
            f.write('<option value="%s" selected="selected">%s</option>'  % (key, val))
        else:
            f.write('<option value="%s">%s</option>'  % (key, val))
    f.write('</select>')
    f.write('<input type="hidden" name="%s:list:default" value="">' % name)
    return f.getvalue()



if __name__=='__main__':
    itms = (('1','1'), ('2','2'), ('3','3'), ('4','4'), ('5','5'), ('6','6'),)
    print multiSelectBox('multi', itms, ['3','5'], size=23)
    print selectBox('multi', itms, '3')